//
//  ApprovalSuggestionViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/13.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class AddDynamicViewController: BaseViewController {
    
    var applyId = ""
    
    var businessType = ""
    
    @IBOutlet weak var trayView: UIView!
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        let height = self.view.width * 329 / 375
        let animation = POPSpringAnimation.init(propertyNamed: kPOPViewFrame)
        animation?.beginTime = CACurrentMediaTime()
        animation?.toValue = NSValue.init(cgRect: CGRect.init(origin: CGPoint.init(x: 0, y: self.view.height - height), size: CGSize(width: self.view.width, height: height)))
        animation?.springSpeed = 50
        animation?.springBounciness = 5
        self.trayView.pop_add(animation, forKey: "t")
    }
    
    @IBOutlet weak var suggestionTextView: XXTextView!
    
    @IBAction func rejectAction(_ sender: UIButton) {
        self.dismiss()
    }
    
    @IBAction func agreeAction(_ sender: UIButton) {
        self.load(with: "agree")
    }
    
    override var names: Array<String> {
        get{
            return ["reject","agree"]
        }
    }
    
    override func success(for name: String) -> NetSuccess{
        
        func suc(json: JSON)->Void{
            if let success = json.contents?["success"]?.rawValue as? Bool {
                if success == true {
                    self.alert("添加成功")
                    NotificationCenter.default.post(name: NSNotification.Name("RefreshPotential"), object: nil)
                    self.dismiss()
                } else {
                    self.alert(json.contents?["errorMsg"]?.rawString())
                }
            }
        }
        
        return suc
    }
    
    override func param(for name: String) -> Dictionary<String,Any>{
        return ["id":applyId,"recordDynamicNotes":self.suggestionTextView.text.trimmingCharacters(in: CharacterSet.illegalCharacters)]
    }
    
    override func path(for name: String) -> String{
        return AddNewDynamic
    }
    
    override func method(for name: String) -> NetMethod{
        return .get
    }
    
    //响应接口的有效性策略,如果参数不合格，则默认根据String提示
    override func strategy(for name: String) -> (Bool,String?,(()->Void)?){
        
        let realText = self.suggestionTextView.text.trimmingCharacters(in: CharacterSet.illegalCharacters)
        
        if realText.count == 0 {
            return (false,"动态内容不能为空",nil)
        }
        return (true,nil,nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        self.view.endEditing(true)
        
        self.dismiss()
        
    }

}
